﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EpicBall.ScreenSystem;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;

namespace EpicBall.Levels.Classes.Enemies
{
    public class StaticTurret : LevelObject
    {

        protected float _delay;
        protected bool _flip, _deadly;
        protected double _timer;
        protected float _speedOfProjectile;

        public StaticTurret(PhysicsGameScreen screen) : this(screen, 1f, false, false, screen.GameStack.Content.Load<Texture2D>("Enemies/turretStatic"), 5f) { }
        public StaticTurret(PhysicsGameScreen screen, float delay) : this (screen, delay, false, false, screen.GameStack.Content.Load<Texture2D>("Enemies/turretStatic"), 5f){}
        public StaticTurret(PhysicsGameScreen screen, float delay, bool flipped):this(screen, delay, flipped, false, screen.GameStack.Content.Load<Texture2D>("Enemies/turretStatic"), 5f){}
        public StaticTurret(PhysicsGameScreen screen, float delay, bool flipped, bool deadly) : this(screen, delay, flipped, deadly, screen.GameStack.Content.Load<Texture2D>("Enemies/turretStatic"), 5f) { }
        public StaticTurret(PhysicsGameScreen screen, float delay, bool flipped, bool deadly, float speedOfProjectile) : this(screen, delay, flipped, deadly, screen.GameStack.Content.Load<Texture2D>("Enemies/turretStatic"), speedOfProjectile) { }
        public StaticTurret(PhysicsGameScreen screen, float delay, bool flipped, bool deadly, Texture2D texture, float speedOfProjectile)
            : base(screen, texture)
        {
            _delay = delay*1000;
            _flip = flipped;
            _timer = 0;
            _deadly = deadly;
            _texture = texture;
            _speedOfProjectile = speedOfProjectile;
        }

        public override void Draw()
        {
            Vector2 displayUnits = ConvertUnits.ToDisplayUnits(_body.Position);
            SpriteEffects effects = SpriteEffects.None;
            
            if(!_flip)
                effects = SpriteEffects.FlipHorizontally;

            _screen.GameStack.SpriteBatch.Draw(_texture, new Rectangle((int)(displayUnits.X - _width / 2), (int)(displayUnits.Y - _height / 2), _width, _height), null, Color.White, 0f, Vector2.Zero, effects, 0.9f);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            _timer += gameTime.ElapsedGameTime.TotalMilliseconds;
            
            
            if (_delay< _timer)
            {
 
                _timer = 0.0;
                Texture2D projectileTexture;

                if (_deadly)
                {
                    projectileTexture = _screen.GameStack.Content.Load<Texture2D>("Enemies/projectileBullet");
                }
                else
                {
                    projectileTexture = _screen.GameStack.Content.Load<Texture2D>("Enemies/projectilePlatform");
                }
                Projectile projectile = new Projectile(_screen, this, _deadly, _flip,  projectileTexture, _speedOfProjectile);
                
                if(!_flip)
                    projectile.Body.Position = this.Body.Position - ConvertUnits.ToSimUnits(new Vector2(25, 0));
                else
                    projectile.Body.Position = this.Body.Position - ConvertUnits.ToSimUnits(new Vector2(-25, 0));

                _screen.AddObject(projectile);
                if (_deadly)
                {
                    projectile.Body.OnCollision += projectile.BulletCollision;
                }
                else
                {
                    projectile.Body.Restitution = 0.6f;
                }
                
            }
        }
    }
}
